package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        if (user == null){
            return null;
        }
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);
        if (roles != null && roles.size() > 0){
            for (Role role : roles) {
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                if (permissions != null && permissions.size() > 0){
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }
        return user;
    }

    @Override
    public User findAllUsername(String username) {
        User user=userDao.findUserByUsername(username);
        return user;
    }

    //用户分页查询
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) throws Exception {
        //调用分页工具
        PageHelper.startPage(currentPage, pageSize);
        Page<User> user = userDao.findPage(queryString);
        for (User user1 : user) {
            Date birthday = user1.getBirthday();
            String s = DateUtils.parseDate2String(birthday);
            user1.setBirthdays(s);
        }
        return new PageResult(user.getTotal(), user.getResult());
    }

    //修改用户状态
    public void updateStation(Integer station, Integer id) {
        userDao.updateStation(station, id);
    }

    //新增用户
    public void add(User user) {
        userDao.add(user);
    }

    //用户回显
    public User findById(Integer id) {
        User user = userDao.findById(id);
        return user;
    }

    //编辑用户
    public void updateById(User user) {
        userDao.updateById(user);
    }

    //修改用户密码
    public void updatePassword(Integer id, String password) {
        userDao.updatePassword(id, password);
    }

    //删除用户
    public void delete(Integer id) {
        userDao.deleteUserAndRole(id);
        userDao.delete(id);
    }

    //查询全部角色
    public List<Role> AddingRoles() {
        return userDao.AddingRoles();
    }

    //查询角色id
    public List<Integer> findByUserAndRole(Integer id) {
        List<Integer> findByUserAndRole = userDao.findByUserAndRole(id);
        return findByUserAndRole;
    }

    //角色授权
    public void RoleAuthorization(Integer userId, Integer[] role) {
        userDao.deleteUserAndRole(userId);
        if (role != null && role.length > 0) {
            for (Integer integer : role) {
                Map<String,Integer> map = new HashMap<>();
                map.put("userId",userId);
                map.put("roleId",integer);
                userDao.RoleAuthorization(map);
            }
        }
    }
}
